package com.QK.cnstudy;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.QK.cnstudy.db.DBManager;
import com.QK.cnstudy.entity.Author;
import com.QK.cnstudy.entity.Sentence;
import com.QK.cnstudy.entity.Work;

public class WorkActivity extends Activity implements OnClickListener {
    private int workId;
    private TextView titleTextView;

    private TextView authorTextView;
    private TextView contentTextView;
    private DBManager dbManager;
    private String titleString;
    private String contentString;

    Button backButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.work);
        getView();
        Bundle bundle = getIntent().getExtras();
        dbManager = CnStudyApp.app.dbManager;
        workId = bundle.getInt("id");
        Work work = dbManager.getWorkById(workId);
        Author author = dbManager.getAuthorById(work.getAuthorId());
        titleString = work.getName();

        contentString = "";

        List<Sentence> sentences = dbManager.getSentences(workId);
        for (Sentence sentence : sentences) {
            contentString += sentence.getContent();
        }

//        contentString += "<br/><br/><br/>";
        titleTextView.setText(work.getName());
        authorTextView.setText("作者：" + author.getName());
        contentString = TranslateActivity.fixFormat(contentString);
        contentTextView.setText(Html.fromHtml(contentString));

        backButton.setOnClickListener(this);
    }

    public void getView() {
        titleTextView = (TextView) findViewById(R.id.state);

        authorTextView = (TextView) findViewById(R.id.author);
        contentTextView = (TextView) findViewById(R.id.content);
        backButton = (Button) findViewById(R.id.back);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.back:
            onBackPressed();
            break;
        default:
            break;
        }

    }
}
